from urllib import request
from urllib import parse
import json
'''
ZH_CN2EN   中译英
EN2ZH_CN   英译中
ZH_CN2JA   中译日
JA2ZH_CN   日译中
'''
def getUrl(src,type):
    typeCall=None
    if type=="0":
        typeCall="AUTO"
    elif type=="1":
        typeCall="ZH_CN2EN"
    elif type=="2":
        typeCall="EN2ZH_CN"
    elif type=="3":
        typeCall="ZH_CN2JA"
    elif type=="4":
        typeCall="JA2ZH_CN"

    data={
        "type": typeCall,
        "i":src,
        "doctype": "json",
        "xmlVersion": "1.8",
        "keyfrom": "fanyi.web",
        "ue": "UTF-8",
        "action": "FY_BY_CLICKBUTTON",
        "typoResult": "true",
    }
    data=parse.urlencode(data).encode("utf8")
    reponse_cont=request.urlopen("http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null",
                                 data)
    result=json.loads(reponse_cont.read().decode("utf8"))
    result_cont=result['translateResult'][0][0]['tgt']
    return result_cont


